﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ThreeByte.ActiveDeck.Config;

namespace ThreeByte.ActiveDeck.Controls
{
    /// <summary>
    /// Interaction logic for CategoriesConfiguration.xaml
    /// </summary>
    public partial class CategoriesConfiguration : UserControl
    {
        private CategoriesConfig cConfig;

        public CategoriesConfiguration(PageConfig config) {
            InitializeComponent();

            DataContext = config;
            cConfig = (CategoriesConfig)config;

            if(cConfig.Theme == @"Content/Controls/CategoryBlueStyle.xaml") {
                ThemeComboBox.SelectedIndex = 1;
            }
            ThemeComboBox.SelectionChanged +=new SelectionChangedEventHandler(ThemeComboBox_SelectionChanged);
        }

        private void AddCategory_Click(object sender, RoutedEventArgs e) {
            CategoriesConfig cConfig = (CategoriesConfig)DataContext;
            cConfig.Categories.Add(new Category("Category 1"));
        }

        private void AddWord_Click(object sender, RoutedEventArgs e) {
            CategoriesConfig cConfig = (CategoriesConfig)DataContext;
            cConfig.Words.Add(new Word());
        }

        private void CategoryListBox_PreviewKeyDown(object sender, KeyEventArgs e) {
            //Delete selected answers on delete key
            if (e.Key == Key.Delete) {
                List<Category> toDelete = new List<Category>();
                foreach (Category c in CategoryListBox.SelectedItems) {
                    toDelete.Add(c);
                }
                foreach (Category c in toDelete) {
                    CategoriesConfig cConfig = (CategoriesConfig)DataContext;
                    cConfig.Categories.Remove(c);
                }
            }
        }

        private void WordListBox_PreviewKeyDown(object sender, KeyEventArgs e) {
            //Delete selected answers on delete key
            if(e.Key == Key.Delete) {
                List<Word> toDelete = new List<Word>();
                foreach(Word w in WordListBox.SelectedItems) {
                    toDelete.Add(w);
                }
                foreach(Word w in toDelete) {
                    CategoriesConfig cConfig = (CategoriesConfig)DataContext;
                    cConfig.Words.Remove(w);
                }
            }
        }

        private void ThemeComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) {
            CategoriesConfig cConfig = (CategoriesConfig)DataContext;
            if (cConfig == null) {
                return;
            }
            if (ThemeComboBox.SelectedIndex == 0) {
                cConfig.Theme = null;
            } else {
                cConfig.Theme = @"Content/Controls/CategoryBlueStyle.xaml";
            }
        }


        private void AnswersButton_Click(object sender, RoutedEventArgs e) {
            QuestionFeedbackControl newFeedbackControl = new QuestionFeedbackControl(cConfig);
            HostForm newDialog = new HostForm(newFeedbackControl);
            if(newDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK) {
                //TODO: Commit this if allowing reversion?
            }
        }
    }
}
